﻿using DotNet.Highcharts.Helpers;
using DotNet.Highcharts.Options;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            handleGET();
        }
        else
        {
            handlePOST();
        }
    }

    private void handleGET()
    {
        //Set values for Industry Name dropdown
        IndustryInfoService industryInfoService = new IndustryInfoService();
        ddl_industryList.DataSource = industryInfoService.getDataSet();
        ddl_industryList.DataTextField = "Industry";
        ddl_industryList.DataValueField = "Symbol";
        ddl_industryList.DataBind();

        //Set values for Date Range Type radiobutton list
        Dictionary<string, string> rbl_dateRangeTypeList = new Dictionary<string, string>();
        rbl_dateRangeTypeList.Add("Weekly", "WeeklyData");
        rbl_dateRangeTypeList.Add("Monthly", "MonthlyData");
        rbl_dateRangeTypeList.Add("Quarterly", "QuarterlyData");
        rbl_dateRangeType.DataTextField = "Key";
        rbl_dateRangeType.DataValueField = "Value";
        rbl_dateRangeType.DataSource = rbl_dateRangeTypeList;
        rbl_dateRangeType.SelectedIndex = 0;
        rbl_dateRangeType.DataBind();

        //Initialize the Graph
        //ClientScript.RegisterStartupScript(this.GetType(), "regJSval", "<script src=\"Pages\\Home\\Startup.js\" language=\"javascript\"></script>");

        //updateGraph();
    }

    private void handlePOST()
    {
        updateGraph();
    }

    //Update Graph
    private void updateGraph()
    {
        //Get Data for Graph
        GraphDataService graphDataService = new GraphDataService();
        List<DataItem> dataItemList = graphDataService.getDataSet(rbl_dateRangeType.SelectedItem.Value, ddl_industryList.SelectedValue, i_fromDate.Value, i_toDate.Value);
        DateTime[] dateArray = (from t in dataItemList select t.Date).ToArray<DateTime>();
        double[] priceArray = (from t in dataItemList select t.Price).ToArray<double>();

        object[] priceObjectArray = new object[priceArray.Length];
        for (int i = 0; i < priceArray.Length; i++)
        {
            priceObjectArray[i] = priceArray[i];
        }

        string[] categories = new string[dateArray.Length];
        for (int i = 0; i < dateArray.Length; i++)
        {
            categories[i] = dateArray[i].Month + "-" + dateArray[i].Day + "-" + dateArray[i].Year;
        }

        DotNet.Highcharts.Highcharts chart = new DotNet.Highcharts.Highcharts("chart")
        .SetTitle(new Title
        {
            Text = "Stock Price Trending",
            X = -20
        })
       .SetXAxis(new XAxis
       {
           Categories = categories           
       })
       .SetSeries(new Series
       {
           Name = "Price", 
           Data = new Data(priceObjectArray)
       })
       .SetTooltip(new Tooltip
       {
           UseHTML = true,
           Shared = true,
           HeaderFormat = "<span style=\"color:orange\">Date:</span>{point.key}<br>"
       });

        ltrChart.Text = chart.ToHtmlString();
    }
}